package com.card.notice;

import java.util.Date;

import com.xiaoao.net.db.DBManager;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.utils.PrimaryKey;
import com.xiaoao.net.utils.PubFunc;
import com.xiaoao.net.utils.TimeUtils;
/**
 * 公告抽象类
 * @author Administrator
 *
 */
public class GlobNotice{
	public static final String NOTICEID = "noticeId";
	public static final String DESC = "desc";
	public static final String STARTDATE = "startDate";
	public static final String ENDDATE = "endDate";
	
	private String noticeId;
	private String desc;
	private Date startDate;
	private Date endDate;
	
	public GlobNotice(String desc, Date startDate, Date endDate){
		this.noticeId = PrimaryKey.getPrimaryKey("c5Notice");
		this.desc = desc;
		this.startDate = startDate;
		this.endDate = endDate;
	}
	
	public GlobNotice(Entity entity) {
		this.noticeId = getStringProperty(NOTICEID, entity);
		this.desc = getStringProperty(DESC, entity);
		this.startDate = new Date(getLongProperty(STARTDATE, entity));
		this.endDate = new Date(getLongProperty(ENDDATE, entity));
	}
	
	public String getStringProperty(String name, Entity entity) {
		return PubFunc.toString(entity.getProperty(name));
	}
	
	public long getLongProperty(String name, Entity entity) {
		return (long)entity.getProperty(name);
	}
	public void saveDB() {
		Entity entity = new Entity(NoticeManager.T_NOTICE_DATA,noticeId);
		entity.setProperty(GlobNotice.NOTICEID, noticeId);
		entity.setProperty(GlobNotice.DESC, desc);
		entity.setProperty(GlobNotice.STARTDATE, startDate.getTime());
		entity.setProperty(GlobNotice.ENDDATE, endDate.getTime());
		DBManager.getInstance().saveOrUpdate(entity);
	}
	
	/**
	 * 判断是否符合发送条件
	 * @return
	 */
	public boolean isFit() {
		Date now = new Date();
		return now.after(this.startDate) && now.before(this.endDate);
	}
	
	public String getNoticeId() {
		return noticeId;
	}
	public void updateNoticeId(){
		this.noticeId = PrimaryKey.getPrimaryKey("c5Notice");
	}
	public String getDesc() {
		return desc;
	}
	
	public String getStartDate() {
		return TimeUtils.ToString(startDate);
	}
	
	public String getEndDate() {
		return TimeUtils.ToString(endDate);
	}
	
}
